﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using IndirectX.Direct3D11;

namespace IndirectX.DXGI
{


    public class SwapChain : IDisposable
    {
        private Factory _factory;
        private SwapChainDescription _description;

        public Texture2D BackBuffer;

        public SwapChain(Factory factory, Device device, SwapChainDescription description)
        {
            _factory = factory;
            _description = description;

            BackBuffer = new Texture2D(device, new Texture2DDescription() { });
        }

        public T GetParent<T>()
        {
            return (T)(object)_factory;
        }

        public bool IsFullScreen;

        public void ResizeBuffers(int count, int width, int height, Format format, SwapChainFlags flags)
        {
            if (width == 0 || height == 0)
            {
                var form = System.Windows.Forms.Control.FromHandle(_description.OutputHandle);
                
                if (width == 0)
                {
                    width = form.ClientSize.Width;
                }
                if (height == 0)
                {
                    height = form.ClientSize.Height;
                }
            }
            
            BackBuffer.Width = width;
            BackBuffer.Height = height;
            
            BackBuffer.Ptr = Marshal.AllocHGlobal(BackBuffer.Width * BackBuffer.Height * 4);
            BackBuffer.Data = new byte[BackBuffer.Width * BackBuffer.Height *4];
            
            for (int i = 0; i < BackBuffer.Data.Length; i++)
            {
                if (i % 4 == 2)
                {
                    BackBuffer.Data[i] = 255;
                }
                if (i % 4 == 3)
                {
                    BackBuffer.Data[i] = 255;
                }
            }
        }

        public void Dispose()
        {
        }

        [System.Runtime.InteropServices.DllImport("gdi32.dll")]
        static extern bool BitBlt(IntPtr hdc, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, uint dwRop);

        public  void Present(int syncInterval, PresentFlags flags)
        {
            var form = System.Windows.Forms.Control.FromHandle(_description.OutputHandle);

            Bitmap bmp = new Bitmap(BackBuffer.Width, BackBuffer.Height, PixelFormat.Format32bppPArgb);
            BitmapData data = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.WriteOnly, bmp.PixelFormat);
   
            System.Runtime.InteropServices.Marshal.Copy(BackBuffer.Data, 0, data.Scan0, BackBuffer.Data.Length);

            bmp.UnlockBits(data);

            ((IndirectX.Windows.RenderForm) form).Surface = bmp;
           
            form.Invalidate();


//            System.Drawing.Bitmap bmp = new Bitmap(256, 256, graphics);
//            for (int i = 0; i < 200; i++)
//            {
//
//                bmp.SetPixel(2, i, System.Drawing.Color.Red);
//                bmp.SetPixel(30, i, System.Drawing.Color.White);
//            }
//
//
//
//            var hdc = graphics.GetHdc();
//
//            System.Drawing.Graphics g2 = System.Drawing.Graphics.FromImage(bmp);
//            var hdc2 = g2.GetHdc();
//
//            BitBlt(hdc, 0, 0, bmp.Width, bmp.Height, hdc2, 0, 0, 0x00CC0020);
//            g2.ReleaseHdc(hdc2);

            //  graphics.ReleaseHdc(hdc);



        }
    }
}
